/**
 * Functions specific to managing Google maps
 * @param   void
 * @return  void
 */
Maps.Manager.Google = function() {
  this.parent();
};

/**
 * @extends   maps.MapManager
 */
Maps.Manager.Common.extending = true;
Maps.Manager.Google.prototype = new Maps.Manager.Common();
Maps.Manager.Common.extending = false;

Maps.Manager.Google.prototype.parent = Maps.Manager.Common;

Maps.Manager.Google.prototype.createDirectionsToForm = function(mapId, mapHead, label, button, location) {
  return  '<form method="get" action="http://maps.google.com/maps" class="mapDir mapDirTo" target="_blank">' +
            '<p class="mapDirHead"><strong>'+mapHead+'</strong></p>' +
            '<p><label for="'+mapId+'_mapTxtDirTo">'+label+'</label> <input type="text" name="saddr" class="mapDirField" id="'+mapId+'_mapTxtDirTo" value="" />' +
              '<input type="submit" value="'+button+'" class="mapButton" />' +
              '<input type="hidden" name="lsm" value="1" />' +
              '<input type="hidden" name="daddr" value="'+ location +'" />' +
            '</p>' +
          '</form>';
};

Maps.Manager.Google.prototype.createDirectionsFromForm = function(mapId, mapHead, label, button, location) {
  return  '<form method="get" action="http://maps.google.com/maps" class="mapDir mapDirTo" target="_blank">' +
            '<p class="mapDirHead"><strong>'+mapHead+'</strong></p>' +
            '<p><label for="'+mapId+'_mapTxtDirFrom">'+label+'</label> <input type="text" name="daddr" class="mapDirField" id="'+mapId+'_mapTxtDirFrom" value="" />' +
              '<input type="submit" value="'+button+'" class="mapButton" />' +
              '<input type="hidden" name="lsm" value="1" />' +
              '<input type="hidden" name="saddr" value="'+ location +'" />' +
            '</p>' +
          '</form>';
};

/**
 * Class destructor to prevent memory leaks.
 * @access  public
 * @param   void
 * @return  void
 */
Maps.Manager.Google.prototype.destroy = function() {
  google.maps.Unload();
  this.parent.prototype.destroy.apply(this);
};

/**
 * Creates the function used as the directions links' onclick.
 * @access  public
 * @param   string  Map HTML tag Id
 * @param   string  Label text in directions form
 * @param   string  Button text in directions form
 * @param   string  Location address or geo coordinates
 * @return  bool    False to cancel link click
 */
Maps.Manager.Google.prototype.directionsLinkClick = function(mapId, label, button, location) {
  return function() {
    var div = document.getElementById(mapId+"_directions");
    var otherLinkId = "";
    var mapManager = maps.MapManagerFactory.get("google");
    
    if (!div) {
      return;
    }
    
    if (this.href == "#to") {
      div.innerHTML = mapManager.createDirectionsToForm(mapId, label, button, location);
      otherLinkId = mapId+"_linkFrom";
    } else {
      div.innerHTML = mapManager.createDirectionsFromForm(mapId, label, button, location);
      otherLinkId = mapId+"_linkTo";
    }
    
    this.className = "mapDirLinkOn";
    document.getElementById(otherLinkId).className = "";
    document.getElementById(mapId+"_mapTxtDir").focus();
    div = null;
    
    mapManager = false;
    
    return false;
  };
};

/**
 * Gets an HTML tag Id for a Google map.
 * @access  public
 * @param   object  Google map object
 * @return  string  HTML tag Id of Google map
 */
Maps.Manager.Google.prototype.getMapId = function(map) {
  return map.getContainer().id;
};

/**
 * Determines browser support
 * @access  public
 * @param   void
 * @return  bool    True if browser is supported
 */
Maps.Manager.Google.prototype.isSupported = function() {
  if (this.supported === null) {
    this.supported = google.maps.BrowserIsCompatible();
  }
  return this.supported;
};
